package server

import (
	"net/http"

	Admin "poetry/server/admin"
	Author "poetry/server/author"
	Works "poetry/server/works"
	// "poetry/server/controller"
	"poetry/middleware/jwt"

	"github.com/gin-gonic/gin"
)

// APIServer api服务
type APIServer struct {
	engine *gin.Engine
}

func (a *APIServer) registry() {
	APIServerInit(a.engine)
}

// APIServerInit api接口服务
func APIServerInit(r *gin.Engine) {

	r.GET("/", func(c *gin.Context) {
		c.String(http.StatusOK, "Welcome")
	})

	v1 := r.Group("/v1/api")
	{
		user := v1.Group("/admin")
		{
			user.POST("/register", Admin.HandlerRegister)
			user.POST("/login", Admin.HandlerLogin)
		}

		author := v1.Group("/author").Use(jwt.Auth())
		// author := v1.Group("/author")
		{
			author.GET("/list", Author.GetAuthorList)
			author.GET("/detail", Author.GetAuthorDetail)
			author.POST("/update", Author.GetAuthorUpdate)
		}
		works := v1.Group("/works").Use(jwt.Auth())
		{
			works.GET("/list", Works.GetWorksList)
			works.DELETE("/delete", Works.GetDeleteWorks)
			works.POST("/create", Works.CreateWorks)
		}
	}

}

// Start http server
func (a *APIServer) Start() {
	a.registry()
	a.engine.Run(":8888")
}

// New 初始化
func New() *APIServer {
	return &APIServer{
		engine: gin.Default(),
	}
}
